package lu.med.praxiswahl.wahlumed.client.patienten;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaske;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaskeListener;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;


@SuppressWarnings("serial")
public class KrankmeldungPanel extends JPanel implements
	PatientenSuchmaskeListener, ActionListener
{
    private Main dataManager;
    private JTextField kmf;
    private String matricule = "";
    private JTabbedPane tabbedPane;

    public KrankmeldungPanel(Main dataManager, PatientenSuchmaske pm,
	    JTabbedPane tabbedPane)
    {
	super(new BorderLayout());
	this.tabbedPane = tabbedPane;
	this.dataManager = dataManager;
	pm.addSuchmaskeListener(this);
	kmf = new JTextField("");
	JButton b = new JButton("Speichern");
	b.addActionListener(this);
	JPanel p = new JPanel(new GridLayout(0, 1));
	p.add(b);
	p.add(kmf);
	this.add(p, BorderLayout.NORTH);
    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
	// Auf speichern wurde geklickt
	if (matricule.equals(""))
	    return;
	dataManager.getCustomerContext().getCustomer(matricule);
	dataManager.getCustomerContext().setKrankMeldung(kmf.getText());
	dataManager.getCustomerContext().save_customer();
	JOptionPane.showMessageDialog(dataManager,
		"Patientin wurde gespeichert");
    }

    @Override
    public void modelChanged(String matricule)
    {
	// Fragt den KM Wert des aktuellen Patienten ab
	/*
	 * this.matricule = matricule;
	 * dataManager.getCustomerContext().getCustomer(matricule);
	 * kmf.setText(""); kmf
	 * .setText(dataManager.getCustomerContext().getKrankMeldung(
	 * matricule));
	 */

	if (this.tabbedPane.getSelectedIndex() == 9)
	{
	    this.matricule = matricule;
	    CustomerTBL tmpCust = dataManager.getCustomerContext().getCustomer(
		    matricule);
	    kmf.setText("");
	    kmf.setText(tmpCust.getKrankmeldung_feld());
	}
    }

    /**
     * @return the matricule
     */
    public String getMatricule()
    {
        return matricule;
    }

    /**
     * @param matricule the matricule to set
     */
    public void setMatricule(String matricule)
    {
        this.matricule = matricule;
    }

}
